// pages/shopping/authenticationSelect/authenticationSelect.js
var app = getApp();
var cardImg = app.globalData._network_path + 'card.png';
var cardImg1 = app.globalData._network_path + 'card-1.png';
var is_auth_type= 1; //审核类型 1后台审核 2
var accredit= false; //是否授权 录像录音
Page({
  /**
   * 页面的初始数据
   */
  data: {
    card_bg1: cardImg,
    card_bg2: cardImg1,
    card_bg3: '../../../images/license.png',
    array: [
      "用户认证",
      // "企业认证",
    ],
    index: 0,
    isShow: true,
    realname: '', //是	String	个人认证: 真实姓名 企业认证: 法人姓名
    id_number: '', //是	String	个人认证: 身份证号 企业认证: 身份证号
    business_license_number: '', //是 string 企业认证：营业执照编号
    business_license_pic: '', //否	String	营业执照图片, 企业认证必传
    front_idcard_pic: '', //否	String	身份证正面, 个人认证必传
    back_idcard_pic: '', //否	String	身份证反面, 个人认证必传
    business_license_pic1: '', //否	String	营业执照图片, 企业认证必传
    front_idcard_pic1: '', //否	String	身份证正面, 个人认证必传
    back_idcard_pic1: '', //否	String	身份证反面, 个人认证必传
    auth_type: 1, //认证类型
    url: '',
    status: 1, //审核状态
    status: 0, //状态
    status_name: '确认上传',
    disabled: false, //禁用
    isLoad: false, //数据加载完
    userInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    // 回显数据
    if (options.type == 1) {
      var subData = wx.getStorageSync('subAuthData')
      that.setData({
        realname: subData.realname,
        id_number: subData.id_number,
        front_idcard_pic: app.globalData._network_path + subData.front_idcard_pic,
        front_idcard_pic1: subData.front_idcard_pic,
        back_idcard_pic: app.globalData._network_path + subData.back_idcard_pic,
        back_idcard_pic1: subData.back_idcard_pic,
      });
    }
    that.getUserInfo();

  },
  onShow: function() {
    var that = this;
    that.getAuthInfo();

    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.camera'] && res.authSetting['scope.record']) {
          accredit=true
        } else {
            accredit=false
        }
        if (!res.authSetting['scope.camera']) {
          wx.authorize({
            scope: 'scope.camera',
            success(res) {
              // 用户已经同意小程序使用录音功能，后续调用 wx.camera 接口不会弹窗询问
              // wx.camera()
            },
            fail() {
              wx.showModal({
                title: '实名认证',
                content: '实名认证需要使用摄像头,以及录音功能',
                showCancel: false,
                confirmText: '去设置',
                // confirmColor: '',
                success: function(res) {
                  wx.openSetting({
                    success(res) {

                    }
                  })
                },
                fail: function(res) {},
                complete: function(res) {},
              })
            }
          })
        }
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
            success(res) {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              wx.startRecord()
            }
          })
        }
      }
    })
  },
 //获取用户信息
 getUserInfo: function() {
  var that = this;
  app.ajax({
    url: 'User/User/getUserInfo',
    method: "POST",
    data: {},
    success: function(res) {
      if (res.data.code == 1000) {
        that.setData({
          userInfo: res.data.data
        })
        that.getSystemConfig();
      } else {
        // app.showToast(res.data.message);
      }
    }
  })
},
  //获取实名认证审核方式
  getSystemConfig: function() {
    var that = this;
    app.ajax({
      url: 'Common/Common/getSystemConfig',
      data: {
        'key': 'user_auth_check',
        'user_id':that.data.userInfo.user_id
      },
      success: function(res) {
        var type = 1;
        if (res.data.data == 2) {
          type = 2;
        } else if (res.data.data == 3) {
          type = 3;
        }
         var is_auth_type= type

      }
    })
  },

  /**
   * 选择认证类型
   */
  bindPickerChange: function(e) {
    var index = e.detail.value;
    var auth_type = '';
    if (index == 0) {
      auth_type = 1;
    } else if (index == 1) {
      auth_type = 2;
    }
    this.setData({
      index: e.detail.value,
      auth_type: auth_type,
      realname: '',
      id_number: '',
      business_license_number: '',
      business_license_pic: '',
      front_idcard_pic: '',
      back_idcard_pic: '',
      business_license_pic1: '',
      front_idcard_pic1: '',
      back_idcard_pic1: '',
    })
  },

  /**
   * 个人认证要求/企业认证要求
   */
  clickRequire: function() {
    this.setData({
      isShow: false
    })
  },

  /**
   * 隐藏要求
   */
  requireHide: function() {
    this.setData({
      isShow: true
    })
  },
  /**
   * 个人姓名输入/公司法人输入
   */
  bindNameInput: function(e) {
    this.setData({
      realname: e.detail.value
    })
  },
  /**
   * 个人身份证输入/企业法人身份证输入
   */
  bindNumInput: function(e) {
    this.setData({
      id_number: e.detail.value
    })
  },

  // 统一社会信用代码输入
  bindCnumInput: function(e) {
    this.setData({
      business_license_number: e.detail.value
    })
  },
  /**
   * 上传身份证正面
   */
  uploadPimg1: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        that.setData({
          front_idcard_pic1: ''
        })
        var tempFilePaths = res.tempFilePaths;
        that.upImg(tempFilePaths, 1)
        //var url = that.data.url;
        setTimeout(function() {
          that.setData({
            front_idcard_pic: tempFilePaths
          })
        }, 500);
      }
    })
  },
  /**
   * 上传身份证反面
   */
  uploadPimg2: function() {
    var that = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        that.setData({
          back_idcard_pic1: ''
        })
        var tempFilePaths = res.tempFilePaths;
        that.upImg(tempFilePaths, 2)
        //var url = that.data.url;
        setTimeout(function() {
          that.setData({
            back_idcard_pic: tempFilePaths
          })
        }, 500);
      }
    })
  },
  /**
   * 上传营业执照
   */
  uploadCimg: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        that.setData({
          business_license_pic1: ''
        })
        var tempFilePaths = res.tempFilePaths;
        that.upImg(tempFilePaths, 0)
        //var url = that.data.url;
        setTimeout(function() {
          that.setData({
            business_license_pic: tempFilePaths
          })
        }, 500);
      }
    })
  },
  /**
   * 确认上传
   */
  uploadSure: function() {
    var that = this;
    if (that.data.index == 0) {
      // 个人认证
    } else if (that.data.index == 1) {
      // 企业认证
    }

  },
  //用户认证
  subAuth: function(e) {
    var that = this;
//    app.saveFormId(e.detail.formId);
    var auth_type = that.data.auth_type;
    // var realname = that.data.realname;
    // var id_number = that.data.id_number;
    var realname = e.detail.value.realname;
    var id_number = e.detail.value.id_number;
    var business_license_number = that.data.business_license_number;
    var business_license_pic = that.data.business_license_pic1;
    var front_idcard_pic = that.data.front_idcard_pic1;
    var back_idcard_pic = that.data.back_idcard_pic1;
    if (realname == '') {
      app.showToast('请填写真实姓名');
      return false;
    }
    if (id_number == '') {
      app.showToast('请填写身份证号码');
      return false;
    }
    if (is_auth_type == 2) { //进入
      wx.navigateTo({
        url: '/pages/shopping/faceRecognition/faceRecognition?name=' + realname + '&id_number=' + id_number
      });
      var data = {};
      data.auth_type = auth_type;
      data.check_type = 2,
        data.realname = realname;
      data.id_number = id_number;
      data.business_license_number = business_license_number;
      data.business_license_pic = business_license_pic;
      data.front_idcard_pic = front_idcard_pic;
      data.back_idcard_pic = back_idcard_pic;
      wx.setStorage({
        key: "subAuthData",
        data: data
      });
      return false;
    } else { //后台审核
      app.ajax({
        url: 'User/User/subAuth',
        method: "POST",
        data: {
          auth_type: auth_type, //认证类型 1个人认证 2企业认证
          check_type: 1,
          realname: realname, //个人认证: 真实姓名 企业认证: 法人姓名
          id_number: id_number, //个人认证: 身份证号 企业认证: 身份证号
          business_license_number: business_license_number, //企业认证：营业执照编号
          business_license_pic: business_license_pic, //营业执照图片, 企业认证必传
          front_idcard_pic: front_idcard_pic, //身份证正面, 个人认证，企业认证必传
          back_idcard_pic: back_idcard_pic //身份证反面, 个人认证，企业认证必传
        },
        success: function(res) {
          if (res.data.code == 1000) {
            app.showToast(res.data.message);
            that.getAuthInfo();
          } else {
            app.showToast(res.data.message);
          }
        }
      })
    }


  },
  //图片上传
  upImg: function(tempFilePaths, type) {
    var that = this;
    wx.showLoading({
      title: '上传图片中',
    })
    wx.uploadFile({
      url: app.globalData._url + 'Common/Common/upload',
      filePath: tempFilePaths[0],
      name: 'file',
      success: function(res) {
        wx.hideLoading();
        var res = JSON.parse(res.data);
        if (res.code == 1000) {
          if (type == 0) {
            that.setData({
              business_license_pic1: res.data.url
            })
          } else if (type == 1) {
            that.setData({
              front_idcard_pic1: res.data.url
            })
          } else if (type == 2) {
            that.setData({
              back_idcard_pic1: res.data.url
            })
          }

        } else {
          app.showToast("上传图片失败");
        }
        //do something
      }
    })
  },

  //获取验证信息
  getAuthInfo: function() {
    var that = this;
    app.ajax({
      url: 'User/User/getAuthInfo',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          var auth_type = res.data.data.auth_type,
            realname = res.data.data.realname,
            id_number = res.data.data.id_number,
            business_license_number = res.data.data.business_license_number,
            business_license_pic = res.data.data.business_license_pic_all,
            front_idcard_pic = res.data.data.front_idcard_pic_all,
            back_idcard_pic = res.data.data.back_idcard_pic_all,
            business_license_pic1 = res.data.data.business_license_pic,
            front_idcard_pic1 = res.data.data.front_idcard_pic,
            back_idcard_pic1 = res.data.data.back_idcard_pic,
            status = res.data.data.status,
            status_name = res.data.data.status_name;
          var disabled = false;
          if (auth_type == 1) {
            var index = 0;
          } else if (auth_type == 2) {
            var index = 1;
          }
          if (status == 2 || status == 1) {
            disabled = true;
          }
          that.setData({
            index: index,
            realname: realname,
            id_number: id_number,
            business_license_number: business_license_number,
            business_license_pic: business_license_pic,
            front_idcard_pic: front_idcard_pic,
            back_idcard_pic: back_idcard_pic,
            business_license_pic1: business_license_pic1,
            front_idcard_pic1: front_idcard_pic1,
            back_idcard_pic1: back_idcard_pic1,
            status: status,
            auth_type: auth_type,
            disabled: disabled,
            status_name: status_name
          })
        } else if (res.data.code == -1003) {

        } else {
          app.showToast(res.data.message);
        }
        that.setData({
          isLoad: true
        })
        wx.hideLoading();
      }
    })
  },
  //跳转人脸识别
  faceRecognition() {
    wx.navigateTo({
      url: '../faceRecognition/faceRecognition',
    })
  }
})